#include "iwdg.h"

void IWDG_Init(uint8_t prer,uint16_t rlr) 
{	
	uint16_t timeout_t=0xFFFFU;
	/* enable IRC40K */
  rcu_osci_on(RCU_IRC40K);
	
	/* wait till IRC40K is ready */
  while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K))
  {
	if(timeout_t > 0) timeout_t--;
	else			  break;
  }
	
	/* configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
  fwdgt_config(rlr,prer);				//t = (1/0.625)x(2x500) = 1.6s
	
  fwdgt_write_disable();
  /* After 1.6 seconds to generate a reset */
  fwdgt_enable();
}


void IWDG_Feed(void)
{   
 	/* uncock fwdgt write protect*/
	fwdgt_write_enable();
	/* feed fwdgt */
	fwdgt_counter_reload();	   
}
